﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
namespace DeepEarth.Map.Core.Utilities
{
    public static class MapHelper
    {
        public static Tuple<RasterOverlayCollection, Canvas, VectorLayerCollection, Canvas> InitializeSurfaces(IMap map, bool mapIsWrapped, UIElementCollection baseChildren, out LayerGroup baseLayerGroup, out Layer baseLayer, out Panel overlayCanvas, out Panel interactionSurface)
        {
            var dynamicLayerCollection = new Canvas();
            var vectorLayerCollection = new VectorLayerCollection();
            vectorLayerCollection.MapIsWrapped = mapIsWrapped;
            var labelLayerCollection = new Canvas();
            var rasterOverlayCollection = new RasterOverlayCollection();

            baseLayerGroup = new LayerGroup(map, dynamicLayerCollection, vectorLayerCollection, rasterOverlayCollection, labelLayerCollection);
            baseChildren.Add(rasterOverlayCollection);
            baseChildren.Add(vectorLayerCollection);
            baseChildren.Add(dynamicLayerCollection);
            baseChildren.Add(labelLayerCollection);

            baseLayer = new Layer();
            baseChildren.Add(baseLayer);

            overlayCanvas = new Canvas();
            baseChildren.Add(overlayCanvas);

            interactionSurface = new Grid()
            {
                IsHitTestVisible = false,
                Background = new SolidColorBrush(Colors.Transparent)
            };

            baseChildren.Add(interactionSurface);

            return new Tuple<RasterOverlayCollection, Canvas, VectorLayerCollection, Canvas>(rasterOverlayCollection, dynamicLayerCollection, vectorLayerCollection, labelLayerCollection);
        }

        public static FrameworkElement CreateElement(DataTemplate template, object model)
        {
            FrameworkElement fe = template.LoadContent() as FrameworkElement;
            if (fe != null)
            {
                fe.DataContext = model;
            }
            return fe;
        }

        public static Point OriginFromLogicalCentreAtWidth(Point centreLogical, IMap map, MultiScaleImage multiScaleImage)
        {
            return new Point(centreLogical.X - (map.CurrentViewportLogicalWidth / 2), centreLogical.Y - ((map.CurrentViewportLogicalWidth / (map.AsFrameworkElement.ActualWidth / map.AsFrameworkElement.ActualHeight) / 2)));
        }
    }
}
